/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package meteocal.utility;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import static java.util.Calendar.*;
import javax.json.JsonArray;
import javax.json.JsonObject;

/**
 *
 * @author andrea
 */
public abstract class DateManager {
    
    /**
     * Calculates the offset between a given date and the current date,
     * taking into account the eventuality of different years dates
     * @param date
     * @return the integer offset
     */
    public static int getDayOffset(Calendar date){
        Calendar currentDate = Calendar.getInstance();
        
        //same year
        if (date.get(YEAR) == currentDate.get(YEAR))
            return date.get(DAY_OF_YEAR) - currentDate.get(DAY_OF_YEAR);
        
        //different years
        Calendar lastYearDay = Calendar.getInstance();
        lastYearDay.set(currentDate.get(YEAR), DECEMBER, 31, 23, 59, 59);
        int middleYearsDays = 365*(date.get(YEAR) - currentDate.get(YEAR) - 1);
        
        return getDayOffset(lastYearDay) + middleYearsDays + date.get(DAY_OF_YEAR);       
        
    }
    
    /**
     * Calculates the position of the nearest forecast to the given hour/date in 
     * the JsonArray
     * @param date
     * @param forecast
     * @return the position of the forecast if exists, -1 otherwise
     */
    public static int getHourOffset(Calendar date, JsonArray forecast){
        
        //Converts UTC in Unix Time
        long time = date.getTimeInMillis()/1000;
        
        JsonObject json;
        long head;
        long tail;        
        for(int i = 0;i < forecast.size();i++){
            json = forecast.getJsonObject(i);
            head = json.getJsonNumber("dt").longValue();
            if(time >= head){                
                json = forecast.getJsonObject(i+1);
                tail = json.getJsonNumber("dt").longValue(); 
                if(time > tail)
                    continue;
                return (time - head) <= (tail - time) ? i : i+1;
            }            
        }
        
        return -1;
        
    }

    /**
     * Creates a Calendar object from the value in Unix Time, converted in UTC
     * @param unixTime
     * @return the resulting object
     */
    public static Calendar buildFromUnixTime(long unixTime) {
        
        Calendar date = Calendar.getInstance();
        date.setTimeInMillis(unixTime*1000);
        return date;
        
    }
    
    /**
     * Compares the given date with the current date.
     * @param date
     * @return true if the given date is after today, false if not, taking into 
     * account that the dates can be of different years.
     */
    public static boolean isAfterToday(Calendar date){
        Calendar today = Calendar.getInstance();
        return date.get(Calendar.YEAR) > today.get(Calendar.YEAR)
                || date.get(Calendar.DAY_OF_YEAR) > today.get(Calendar.DAY_OF_YEAR);
    }
    
    /**
     * Creates a represention of the date in "dd/mm/yyyy" format.
     * @param date
     * @return the represention string
     */
    public static String calendarToString(Calendar date){
        SimpleDateFormat sf = new SimpleDateFormat("dd/MM/yyyy");
        return sf.format(date.getTime());
    }
}
